using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;

namespace Light
{
    public class SkillsTables : SingletonMono<SkillsTables>
    {
        private readonly Dictionary<ItemType, AbstractListSO> data = new();
        private readonly string JSONNAME = "Skills";

        private SkillsJsonListData jsonData = new();

        protected override void Awake()
        {
            base.Awake();
            LoadStaticData();

            ////json���ݲ���
            //jsonData.data.Add(new SkillsJsonData { uId = Guid.NewGuid().ToString(), id = "301" });
            //jsonData.data.Add(new SkillsJsonData { uId = Guid.NewGuid().ToString(), id = "302" });
            //jsonData.data.Add(new SkillsJsonData { uId = Guid.NewGuid().ToString(), id = "303" });
            //jsonData.data.Add(new SkillsJsonData { uId = Guid.NewGuid().ToString(), id = "304" });
            //jsonData.data.Add(new SkillsJsonData { uId = Guid.NewGuid().ToString(), id = "305" });

            //SaveDynamicData();
            LoadDynamicData();
        }

        public List<T2> GetData<T1, T2>(ItemType type) where T1 : AbstractListSO, new() where T2 : AbstractItemSO, new()
        {
            if (data.ContainsKey(type))
            {
                data.TryGetValue(type, out var list);
                var result = list as T1;
                return result.GetData<T2>();
            }

            return null;
        }

        public async Task<List<T2>> GetDataAsync<T1, T2>(ItemType type) where T1 : AbstractListSO, new()
            where T2 : AbstractItemSO, new()
        {
            await UniTask.WaitUntil(() => data.Count != 0);

            if (data.ContainsKey(type))
            {
                data.TryGetValue(type, out var list);
                var result = list as T1;
                return result.GetData<T2>();
            }

            return null;
        }

        /// <summary>
        ///     ��ȡ����skills
        /// </summary>
        /// <returns></returns>
        public IEnumerable<SkillsJsonData> GetSkillsItemJsonData()
        {
            return jsonData.data;
        }

        private void LoadDynamicData()
        {
            jsonData = JsonManager.Instance.LoadData<SkillsJsonListData>(JSONNAME);
        }

        private void SaveDynamicData()
        {
            JsonManager.Instance.SaveData(jsonData, JSONNAME);
        }

        private void LoadStaticData()
        {
            //����
            ResManager.Instance.LoadAsync<SkillItemListSO>("SkillItemList", list => { data.Add(ItemType.Skill, list); },
                LoadType.AddressAbles);
        }

        public void AddJsonDataToDataBySkillId(string id)
        {
            SkillsJsonData data = new() { uId = Guid.NewGuid().ToString(), id = id };
            AddJsonDataToData(data);
        }

        public void AddJsonDataToData(SkillsJsonData data)
        {
            jsonData.data.Add(data);
            SaveDynamicData();
        }

        public SkillsJsonData FindJsonDataById(string id, FunctionBarContentType type)
        {
            if (type == FunctionBarContentType.SkillsBarView) return jsonData.data.Find(data => data.id == id);
            return null;
        }

        public SkillItemSO GetSkillByIndex(ItemType type, int index)
        {
            data.TryGetValue(type, out var list);
            if (type == ItemType.Skill)
                return list.GetData<SkillItemSO>()[index];
            return null;
        }

        public SkillItemSO GetSkillBySkillId(ItemType type, string id)
        {
            List<SkillItemSO> skillsData = null;
            data.TryGetValue(type, out var list);
            if (type == ItemType.Skill)
                skillsData = list.GetData<SkillItemSO>();
            foreach (var skill in skillsData)
                if (skill.id == id)
                    return skill;
            return null;
        }
    }
}